﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web.Mvc;
using System.Xml;

namespace CMSMVC4.Areas.Main.Models
{
    public class RssActionResults : ActionResult
    {
        //
        // GET: /SitemapActionResult/
        private List<ItemRss> _posts;
        public void RssActionResult(List<ItemRss> posts)
        {
            this._posts = posts;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            context.HttpContext.Response.ContentType = "application/rss+xml";
            using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Output))
            {
                writer.WriteStartElement("rss");
                writer.WriteAttributeString("version", "2.0");
                writer.WriteStartElement("channel");
                writer.WriteElementString("title","Công nghệ quét Laser 3D");
                writer.WriteElementString("description", "3DSCAN VIETNAM – Nhà cung cấp dịch vụ quét và xây dựng mô hình 3D sử dụng công nghệ laser chuyên nghiệp tại Việt Nam. ");
                writer.WriteElementString("link", "http://3dscan.com.vn/");
                writer.WriteElementString("copyright", "3DSSCAN VIETNAM");
                writer.WriteElementString("generator", "http://3dscan.com.vn/rss/");
                writer.WriteElementString("pubDate", DateTime.Now.ToString(CultureInfo.InvariantCulture));
                writer.WriteElementString("lastBuildDate", DateTime.Now.ToString(CultureInfo.InvariantCulture));
                foreach (ItemRss post in this._posts)
                {
                    writer.WriteStartElement("item");

                    writer.WriteStartElement("title");
                    writer.WriteCData(post.title);
                    writer.WriteEndElement();

                    writer.WriteStartElement("description");
                    writer.WriteCData(post.description);
                    writer.WriteEndElement();

                    writer.WriteElementString("link",   string.Format("http://3dscan.com.vn{0}", post.link));
                    writer.WriteElementString("pubDate", post.pubDate.ToString(CultureInfo.InvariantCulture));
                    writer.WriteEndElement();
                }
                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.Flush();
                writer.Close();
            }
        }
    }
}